package com.itheima.service.impl;

import com.itheima.exception.DeptException;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.service.DeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Resource
    private DeptMapper deptMapper;

    @Override
    public List<Dept> queryAll() {
        List<Dept> deptList = deptMapper.queryAll();
        return deptList;
    }

    //根据id删除值
    @Override
    public void deleteById(Integer id) {
        Emp emp = deptMapper.selectByDeptId(id);
        if (emp != null) {
            throw new DeptException("当前部门下有员工，不能直接删除！");
        }

        deptMapper.deleteById(id);
    }

    //添加部门
    @Override
    public void addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.addDept(dept);
    }

    //根据ID查询部门
    @Override
    public Dept selectById(Integer id) {
        Dept dept = deptMapper.selectById(id);
        return dept;
    }

    //根据ID修改部门
    @Override
    public void updateById(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateById(dept);
    }
}
